package com.android.mangalib.activity;

import java.text.SimpleDateFormat;

import com.android.mangalib.activity.R;
import com.android.mangalib.constant.Constant;
import com.android.mangalib.object.Tome;
import com.android.mangalib.parser.XMLParser;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

/**
 * This activity is called when the user click on a tome in a collection
 * 
 */
public class TomeActivity extends Activity {

    @Override
    /**
     * Main method, called when the activity is created
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tome);
        
        // Disable the "previous" button and remove the logo 
        getActionBar().setDisplayHomeAsUpEnabled(false);
        getActionBar().setDisplayShowHomeEnabled(false);
        
        // Set the title of the activity as the title of the collection + the title of the tome
        final String collectionTitle = getIntent().getStringExtra(Constant.COLLECTION_ACTIVITY);
        final String tomeTitle = getIntent().getStringExtra(Constant.TOME_ACTIVITY);
        
        setTitle(collectionTitle + " " + tomeTitle);
        
        try {
        	// Get the information about the tome
            Tome tome = XMLParser.getTome(this, collectionTitle, tomeTitle);
            
            // Creation of the view
            CheckBox checkBoxOwned = (CheckBox) findViewById(R.id.checkBox);
            checkBoxOwned.setText("Owned");
            checkBoxOwned.setChecked(tome.isOwned());
            checkBoxOwned.setClickable(false);
            TextView textViewPrice = (TextView) findViewById(R.id.textViewPrice);
            textViewPrice.setText("Price : " + tome.getPrice() + " €");
            TextView textViewDate = (TextView) findViewById(R.id.textViewDate);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
            simpleDateFormat.applyPattern(Constant.FORMAT_DATE);
            textViewDate.setText("Publication date : " + simpleDateFormat.format(tome.getPublication_date()));
            
            
        }
        catch (Exception e) {
        	Log.v("ERROR", e.getMessage());
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tome, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
